#parse argument

architecture=$1
matrix_decompressed_file=$2
matrix_comp_file=$3
output_dir=$4
lorem_ipsum_file=$5

permutation_str=""

head -c 32M /dev/urandom > $output_dir/rndfile_0.lsbs
head -c 32M /dev/urandom > $output_dir/rndfile_1.lsbs


# calculate size of each block

lorem_ipsum_file_size=$(du -b $lorem_ipsum_file | cut -f1)
length0=$(expr $lorem_ipsum_file_size / 2 )
offset0=0
offset1=$(expr $offset0 + $length0 )
length1=$(expr $lorem_ipsum_file_size - $offset1 )
tag_blocks=25

# get capacity

str="
xrnlib-cli \
    --logging-conf -lib-info-log $output_dir/capacity_0.json \
    --get-capacity -disable-folding -envelop-file $output_dir/rndfile_0.lsbs
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --logging-conf -lib-info-log $output_dir/capacity_1.json \
    --get-capacity -disable-folding -envelop-file $output_dir/rndfile_1.lsbs
"
echo ""; echo $str; echo ""
eval $str

capacity_first_block=$(cat $output_dir/capacity_0.json | grep byte_capacity | sed 's/^.*:.//' | tr -d '}' | tr -d ' ' )
capacity_second_block=$(cat $output_dir/capacity_1.json | grep byte_capacity | sed 's/^.*:.//' | tr -d '}' | tr -d ' ' )

if [ "$capacity_first_block" == "$length0" ] ; then
   echo FAIL
   exit
fi

if [ "$capacity_second_block" == "$length1" ] ; then
   echo FAIL
   exit
fi

# generate blocks

str="
xrnlib-cli \
    --encoding-conf -compiled-param -no-password \
    --make-steganography-block -offset $offset0 -length $length0 -tag $tag_blocks -source-file $lorem_ipsum_file -embedded-file $output_dir/embedded_block_file0.xf -envelop-file $output_dir/rndfile_0.lsbs -disable-folding
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --encoding-conf -compiled-param -no-password \
    --make-steganography-block -offset $offset1 -length $length1 -tag $tag_blocks -source-file $lorem_ipsum_file -embedded-file $output_dir/embedded_block_file1.xf -envelop-file $output_dir/rndfile_1.lsbs -disable-folding
"
echo ""; echo $str; echo ""
eval $str

# extract blocks

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --extract-steganography-block -embedded-file $output_dir/embedded_block_file0.xf -block-file $output_dir/block_file0.xf
"
echo ""; echo $str; echo ""
eval $str
exit

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --extract-steganography-block -embedded-file $output_dir/embedded_block_file1.xf -block-file $output_dir/block_file1.xf
"
echo ""; echo $str; echo ""
eval $str
exit

# merge block 0 and 1

str="
xrnlib-cli \
    --combine-blocks -block-file $output_dir/block_file0.xf -block-file $output_dir/block_file1.xf -combined-block-file $output_dir/block_file01.xf
"
echo ""; echo $str; echo ""
eval $str

# strip header and check against the original file

str="
xrnlib-cli \
    --strip-header-from-block -block-file $output_dir/block_file01.xf -plain-text $output_dir/stripped_lorem_ipsum.txt
"
echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/stripped_lorem_ipsum $lorem_ipsum_file && echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str

